/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.MemberDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import service.AddCollection;

@WebServlet("/cs")
public class CollectServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId=Integer.parseInt(req.getParameter("bookid"));
        HttpSession session=req.getSession();
        String name=(String)session.getAttribute("username");
        int memberId=new MemberDao().getMemberByName(name).getId();
        Boolean bool=new AddCollection().addCollection(bookId,memberId);
        resp.setContentType("text/html;charset=utf-8");
        if(bool==false){
            resp.getWriter().println("<script language='javascript'>alert('您已将本书添加到收藏夹！');window.location.href='gbbi?id="+bookId+"';</script>");
        }
        else
        {           
            resp.getWriter().println("<script language='javascript'>alert('添加成功！');window.location.href='gbbi?id="+bookId+"';</script>");
        }
        resp.getWriter().println("<a href='gbbi?id="+bookId+"'>返回</a>");
    }
    
}
